package com.egobuy.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.egobuy.cms.mapper.ContentMapper;
import com.egobuy.cms.service.IContentService;
import com.egobuy.entity.Content;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egobuy.pojo.BuyResult;
import com.egobuy.pojo.EgoBuyPager;
import com.egobuy.util.JsonTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 戴着假发的程序员
 * @since 2021-11-22
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
    @Autowired
    private ContentMapper contentMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public BuyResult<Content> saveContent(Content content) {
        BuyResult result=BuyResult.ok();
        Date date=new Date();
        //补全数据
        content.setCreated(date);
        content.setUpdated(date);
        contentMapper.insert(content);
        result.setData(content);
        return result;
    }

    @Override
    public BuyResult<EgoBuyPager<Content>> queryByPage(int page, int size, Content content) {
        BuyResult<EgoBuyPager<Content>> result=BuyResult.ok();
        EgoBuyPager<Content> pager=new EgoBuyPager<>();
        pager.setCurrent(page);
        pager.setSize(size);
        //
        QueryWrapper<Content> queryWrapper=new QueryWrapper();
        if (content!=null){
            if (content.getCategoryId()!=null&& content.getCategoryId()!=0){
                queryWrapper.eq("category_id",content.getCategoryId());
            }
            if (content.getContent()!=null&&!"".equals(content.getContent())){
                queryWrapper.like("content","%"+content.getContent()+"%");
            }
        }
        pager=contentMapper.selectPage(pager,queryWrapper);
        long maxNums = pager.getPages();
        int pageStart=1;
        int pageEnd=10;
        if(page-4>1){
            pageStart=page-4;
        }
        if (page+5>maxNums){
            pageEnd= (int) maxNums;
        }
        pager.setPageStart(pageStart);
        pager.setPageEnd(pageEnd);
        result.setData(pager);
        return result;
    }

    /**
     * 查询
     * 返回前端动态链接
     * @param categoryId
     * @return
     */
    @Override
    public BuyResult<List<Content>> queryByCategoryId(long categoryId) {
        BuyResult result=BuyResult.ok();

        String key = "CONTENT_"+categoryId;
        if (redisTemplate.hasKey(key)){
            String json = redisTemplate.opsForValue().get(key);
            if (json!=null&& !"".equals(json)){
                List<Content> contents = JsonTools.string2Collection(json, List.class, Content.class);
                result.setData(contents);
                return result;
            }
        }

        QueryWrapper<Content> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<Content> contents = contentMapper.selectList(queryWrapper);
        String s = JsonTools.obj2String(contents);
        redisTemplate.opsForValue().set(key,s);
        redisTemplate.expire(key,30+(new Random(10).nextInt()), TimeUnit.MINUTES);

        result.setData(contents);
        return result;
    }
}
